package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Bizlogic.CAManager;
import DAO.MySQL.OrderDAOImpl;
import DAO.MySQL.OrderDetailDAOImpl;
import Model.OrderDetail;
import Model.Product;

import com.rms.util.Constant;

/**
 * @author TONY
 * 
 */
public class SearchOrderDetail extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CAManager caManager = new CAManager();
		 String startstr=request.getParameter("startdate");
		 String endstr=request.getParameter("enddate");
		 SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		 Date startdate=null;
		 Date enddate=null;
		 System.out.print(startstr);
		try {
			startdate = sdf.parse(startstr);
			enddate=sdf.parse(endstr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
		List<OrderDetail> list = new ArrayList<OrderDetail>();
		if("".equals(request.getParameter("ordernum").trim()))
		{
			OrderDetailDAOImpl ordi=new OrderDetailDAOImpl();
			 list=ordi.getOrderDetailsBtwDate(startdate, enddate);
		}
		else
		{
			int ordernum = Integer.parseInt(request.getParameter("ordernum"));
			OrderDetail od = caManager.getOrderDetailByOrderId(ordernum);
			 list = new ArrayList<OrderDetail>();
			list.add(od);
		}
		request.setAttribute("list", list);
		request.getRequestDispatcher("GetTransactionHistory.jsp").forward(
				request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String ua = request.getHeader(Constant.HEAD);
		PrintWriter out = response.getWriter();
		if (Constant.MIDP_USER_AGENT.equals(ua)) {
			
			 CAManager caManager = new CAManager();
			 String startstr=request.getParameter("startdate");
			 String endstr=request.getParameter("enddate");
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
			 Date startdate=null;
			 Date enddate=null;
			try {
				startdate = sdf.parse(startstr);
				enddate=sdf.parse(endstr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		 
			
			//OrderDetailDAOImpl oddi=new OrderDetailDAOImpl();
			List<OrderDetail> list = new ArrayList<OrderDetail>();
			if("0".equals(request.getParameter("ordernum").trim()))
			{
				OrderDetailDAOImpl ordi=new OrderDetailDAOImpl();
				list=ordi.getOrderDetailsBtwDate(startdate, enddate);
			}
			else
			{
				int ordernum = Integer.parseInt(request.getParameter("ordernum"));
				OrderDetail od = caManager.getOrderDetailByOrderId(ordernum);
				list = new ArrayList<OrderDetail>();
				list.add(od);
			}
			for (OrderDetail orderDetail : list) {
				   
				  // System.out.println(list.size());
				   out.print("Order Number is     "+orderDetail.getOrdernum());
				   out.print("      This order is done by "+orderDetail.getEmployeename());
				   out.print("     This order is issued on   "+orderDetail.getOrderdate());
				   out.print("     This order used following parts   ");
				   List<Product> listpro=null;
				    listpro=orderDetail.getProduct();	
				   if(listpro!=null)
				   {
				   for (Product product : listpro) {
				   
						 out.print("     part name   "+product.getProductname());
						 out.print("     part quantity   "+product.getQuantity());
				   }
				
			}
			
			  
			 
			 
				
			
			  
		}

		
		

			// EmployeeDAO ed=new EmployeeDAOImpl();

		}
	}

}
